//
//  ZXDateExtension.swift
//  ZXExtension
//
//  Created by ZX on 2025/5/10.
//

import Foundation


public extension Date {
    func toStr(_ format: String) -> String {
        // 创建一个 DateFormatter 对象
        let formatter = DateFormatter()
        formatter.dateFormat = format
        formatter.timeZone = TimeZone.current
        let timeStr = formatter.string(from: self)
        return timeStr
    }
    
    var isToday: Bool {
        let calendar = Calendar.current
        let currentDate = Date()
        // 比较日期是否相同
        return calendar.isDate(self, inSameDayAs: currentDate)
    }
    
    var isYesterday: Bool {
        self.isTheDay(-1)
    }
    func isTheDay(_ distanceToday: Int) -> Bool {
        let calendar = Calendar.current
        let currentDate = Date()
        // 获取指定的日期
        if let theDay = calendar.date(byAdding: .day, value: distanceToday, to: currentDate) {
            // 比较日期是否相同
            return calendar.isDate(self, inSameDayAs: theDay)
        }
        return false
    }
}
